package com.huawei.petapp2.activity;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.huawei.petapp2.R;
import com.huawei.petapp2.activity.widget.BaseActivity;
import com.huawei.petapp2.activity.widget.NoScrollViewPager;
import com.huawei.petapp2.adapter.ViewPagerAdapter;
import com.huawei.petapp2.category.CategoryFragment;
import com.huawei.petapp2.cart.CartFragment;
import com.huawei.petapp2.community.CommunityFragment;
import com.huawei.petapp2.fragment.HomeFragment;
import com.huawei.petapp2.fragment.MineFragment;
import com.huawei.petapp2.network.core.ResponseEntity;
import com.jaeger.library.StatusBarUtil;

import java.util.ArrayList;

public class HomeActivity extends BaseActivity implements View.OnClickListener {
    private NoScrollViewPager mViewPager;
    private LinearLayout mLlHome;
    private LinearLayout mLlClassify;
    private LinearLayout mLlShoppingCart;
    private LinearLayout mLlMine;
    private LinearLayout mLlStore;
    private View v;
    @Override
    protected int setLayoutId() {
        return R.layout.app_act_main;
    }

    @Override
    public void initView() {
        v=getWindow().getDecorView();
        mViewPager = findViewById(R.id.app_act_main_vp);
        mLlHome = findViewById(R.id.app_include_bottom_main_ll_home);
        mLlClassify = findViewById(R.id.app_include_bottom_main_ll_classify);
        mLlShoppingCart = findViewById(R.id.app_include_bottom_main_ll_cart);
        mLlMine = findViewById(R.id.app_include_bottom_main_ll_mine);
        mLlStore=findViewById(R.id.app_include_bottom_main_ll_store);
        int id= getIntent().getIntExtra("id",  0);
        System.out.println("跳转的id为");
        if(id==4){
            mViewPager.setCurrentItem(3);
        }
    }

    @Override
    public void initData() {
        //添加Fragment
        ArrayList<androidx.fragment.app.Fragment> mFragments = new ArrayList<androidx.fragment.app.Fragment>();
        mFragments.add(new HomeFragment());
        mFragments.add(new CategoryFragment());
        mFragments.add(new CommunityFragment());
        mFragments.add(new CartFragment());
        mFragments.add(new MineFragment());

        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), mFragments);
        mViewPager.setAdapter(adapter);
        mViewPager.setScroll(false);
        mLlHome.setSelected(true);
    }

    @Override
    public void initEvent() {
        mLlHome.setOnClickListener(this);
        mLlClassify.setOnClickListener(this);
        mLlStore.setOnClickListener(this);
        mLlShoppingCart.setOnClickListener(this);
        mLlMine.setOnClickListener(this);
    }

    protected void onRestart() {
        Log. e( "zz", "onRestart");
        int id= getIntent().getIntExtra("id",  0);
        System.out.println("跳转的id为");
        if(id==4){
            mViewPager.setCurrentItem(3);
        }
        super.onRestart();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //首页
            case R.id.app_include_bottom_main_ll_home:
                StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
                StatusBarUtil.setLightMode(this);
                mViewPager.setCurrentItem(0);
                tabSelected(mLlHome);
                break;
            //分类
            case R.id.app_include_bottom_main_ll_classify:
                StatusBarUtil.setLightMode(this);
                StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 0);
                mViewPager.setCurrentItem(1);
                tabSelected(mLlClassify);
                break;
            //店铺
            case R.id.app_include_bottom_main_ll_store:
                StatusBarUtil.setLightMode(this);
                StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 0);
                mViewPager.setCurrentItem(2);
                tabSelected(mLlStore);
                break;
            //购物车
            case R.id.app_include_bottom_main_ll_cart:
                StatusBarUtil.setLightMode(this);
                StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 0);
                mViewPager.setCurrentItem(3);
                tabSelected(mLlShoppingCart);
                break;

            //我的
            case R.id.app_include_bottom_main_ll_mine:
                StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary), 0);
                StatusBarUtil.setDarkMode(this);
                mViewPager.setCurrentItem(4);
                tabSelected(mLlMine);
                break;
            default:
                StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
                mViewPager.setCurrentItem(0);
                tabSelected(mLlHome);
                break;
        }
    }

    /**
     * Tab选中
     *
     * @param linearLayout
     */
    private void tabSelected(LinearLayout linearLayout) {
        mLlHome.setSelected(false);
        mLlClassify.setSelected(false);
        mLlShoppingCart.setSelected(false);
        mLlStore.setSelected(false);
        mLlMine.setSelected(false);
        linearLayout.setSelected(true);
    }

    /**
     * 退出应用
     */
    private long clickTime = 0; //记录第一次点击的时间

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - clickTime) > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                clickTime = System.currentTimeMillis();
            } else {
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onBusinessResponse(String apiPath, boolean success, ResponseEntity rsp) {

    }

    @Override
    protected void setStatusBar() {
        super.setStatusBar();
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        StatusBarUtil.setLightMode(this);
    }

    /*private TabLayout mTabLayout;
    private Fragment[]mFragmensts;

    private static final String TAG ="HomeActivity";
    private MyViewPager mShuffling;
    private ShufflingAdapter mShufflingAdapter;
    private Handler mHandler;
    private LinearLayout mLinearLayout;
    private static List<Integer> sList = new ArrayList<>();
    private boolean mIsTouch = false;

    static {
        sList.add(R.drawable.p1);
        sList.add(R.drawable.p2);
        sList.add(R.drawable.p3);
        sList.add(R.drawable.p4);
        sList.add(R.drawable.p5);
        sList.add(R.drawable.p6);
        sList.add(R.drawable.p7);
    }



    public int getLayoutId() {
        return R.layout.app_fragment_home;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.app_include_home_header);
        initView();
        mHandler = new Handler();

    }

    @Override
    public void onAttachedToWindow() {
        Log.d(TAG, "onAttachedToWindow...");
        super.onAttachedToWindow();
        mHandler.post(mRunnable);
    }

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (!mIsTouch) {
                int currentItem = mShuffling.getCurrentItem();
                mShuffling.setCurrentItem(++currentItem, false);
            }
            mHandler.postDelayed(this, 3000);
        }
    };

    @Override
    public void onDetachedFromWindow() {
        Log.d(TAG, "onDetachedFromWindow...");
        super.onDetachedFromWindow();
        mHandler.removeCallbacks(mRunnable);
    }

    private void initView() {
//        1.找到ViewPager组件
        mShuffling = findViewById(R.id.shuffling);
        mShuffling.setOnViewPagerTouchListen(new MyViewPager.OnViewPagerTouchListen() {
            @Override
            public void onViewPagerTouch(boolean isTouch) {
                mIsTouch = isTouch;
            }
        });
//        2.设置适配器
        mShufflingAdapter = new ShufflingAdapter();
        mShufflingAdapter.setData(sList);
        mShuffling.setAdapter(mShufflingAdapter);
        mShuffling.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int realPosition = 0;
                if (mShufflingAdapter.getDataRealSize() != 0) {
                    realPosition = position % mShufflingAdapter.getDataRealSize();
                }
                selectedPoint(realPosition);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mLinearLayout = findViewById(R.id.points);
        initPoints();
        mShuffling.setCurrentItem(mShufflingAdapter.getDataRealSize() * 100 - 1, false);
    }

    private void selectedPoint(int realPosition) {

        for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
            View point = mLinearLayout.getChildAt(i);
            if (i == realPosition) {
                point.setBackgroundResource(R.drawable.shape_point_selected);
            } else {
                point.setBackgroundResource(R.drawable.shape_point_normal);
            }
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initPoints() {
        for (int i = 0; i < mShufflingAdapter.getDataRealSize(); i++) {
            View point = new View(this);
            point.setBackgroundResource(R.drawable.shape_point_normal);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(40, 40);
            params.leftMargin = 20;
            point.setLayoutParams(params);
            mLinearLayout.addView(point);
        }
    }*/

}